package com.vm.osm.business.controller;

import com.vm.osm.business.common.Constants;
import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.dto.rsp.message.RspMessagePage;
import com.vm.osm.business.entity.OsmMessage;
import com.vm.osm.business.service.IOsmMessageService;
import com.vm.osm.business.util.UserDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chenzui on 2018/3/9.
 */
@RestController
@RequestMapping("/v1.0")
@Api(value = "OsmMessageController", description = "客户消息模块", tags = {"OsmMessageController"})
public class OsmMessageController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());

    @Autowired
    private IOsmMessageService osmMessageService;

    @ApiOperation(value = "发送请求",notes = "发送请求,邀请成为服务商")
    @RequestMapping(value = "/message/send",method = RequestMethod.POST)
    public void sendMessage(
            @ApiParam(required=true, name="rela_company_id", value="抢单开关") @RequestParam("rela_company_id") String relaCompanyId,
            @ApiParam(required=true, name="content", value="发送方消息内容") @RequestParam("content") String content,
            @ApiParam(required=true, name="rela_content", value="接收展示的标题模版") @RequestParam("rela_content") String receiveTitle,
            @ApiParam(required=true, name="send_title", value="发送方展示的标题模版") @RequestParam("send_title") String sendTitle,
            HttpServletRequest request){
        LOG.info("发送请求");
        String mac = request.getHeader(Constants.MAC_KEY);
        //企业编号
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        String staffId = UserDataUtil.getStaffId(request);

        String staffName = UserDataUtil.getStaffName(request);

        String enterpriseName = UserDataUtil.getUserSelectedEnterpriseName(request);

        osmMessageService.sendMessage(enterpriseId,relaCompanyId,enterpriseName,staffName,staffId,content,receiveTitle,sendTitle);

    }
    @ApiOperation(value = "查询消息",notes = "查询消息")
    @RequestMapping(value = "/message/get_messages",method = RequestMethod.GET)
    public RspMessagePage getMessagePage(
            HttpServletRequest request,
            @ApiParam(required=true, name="page_num", value="分页数") @RequestParam("page_num") Integer pageNum,
            @ApiParam(required=true, name="page_size", value="分页大小") @RequestParam("page_size") Integer pageSize,
            @ApiParam(name="type", value= "1发出2接收") @RequestParam(value = "type",required = false) String type){
        RspMessagePage rspMessagePage = null;
        String mac = request.getHeader(Constants.MAC_KEY);
        //企业编号
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        if("1".equals(type)){
            rspMessagePage = osmMessageService.querySendMessages(pageNum,pageSize,enterpriseId);
        }else {
            rspMessagePage = osmMessageService.queryReceiveMessages(pageNum,pageSize,enterpriseId);
        }
        return rspMessagePage;
    }

    @ApiOperation(value = "处理收到的消息",notes = "处理收到的消息")
    @RequestMapping(value = "/message/handle_action",method = RequestMethod.PUT)
    public void handleMessageAction(HttpServletRequest request,
        @ApiParam(required = true,name = "type",value = "1同意2拒绝") @RequestParam("type") String type,
        @ApiParam(required = true,name = "msg_id",value = "消息ID") @RequestParam("msg_id") String msgId){

        OsmMessage osmMessage = osmMessageService.selectByPrimaryKey(msgId);

        if("1".equals(type)){
            osmMessage.setStatus(new Byte("2"));
        }else if("2".equals(type)) {
            osmMessage.setStatus(new Byte("3"));
        }
        osmMessageService.updateByPrimaryKeySelective(osmMessage);

    }
}
